package controllers;

import helpers.DateHelper;
import helpers.IdUniqueHelper;
import helpers.LanguageHelper;
import helpers.PaginatorHelper;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.AuthorModel;
import models.QuestionModel;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import systems.ConfigSystem;
import systems.RecentlyUpdate;

import admin.models.CategoryModel;

import debugs.QuestionDebug;

import entities.Author;
import entities.Category;
import entities.Question;
import entities.SiteConfig;

public class HomeServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		SiteConfig config = ConfigSystem.getConfigs();
		
		//Get lasetst news and paginator
		int page = 1;
		try {
			page = Integer.parseInt((String) request.getParameter("page"));
		} catch (Exception e) {}
		
		boolean nextPage = false;
		boolean prevPage = false;
		int limit = 20;
		
		
		QuestionModel questionModel = QuestionModel.getInstance();
		List<Question> questions = RecentlyUpdate.getQuestions();
		
		if (page > 1 || questions.size() < limit) {
			questions = questionModel.list(page, limit);
		}
		
		if (questions.size() == limit || page == 1) {
			nextPage = true;
		}
		
		if (page > 1) {
			prevPage = true;
		}
		
		//Get paginate
		String queryString = request.getQueryString();
		String requestURI = request.getRequestURI();
		PaginatorHelper paginator = new PaginatorHelper(queryString);
		paginator.setUrl(requestURI);
		
		List<String> aIds = new ArrayList<String>();
		List<String> cIds = new ArrayList<String>();
		
		for (int i = 0; i < questions.size(); i++) {
			if (!questions.get(i).getAuthor().isEmpty())
				aIds.add(questions.get(i).getAuthor());
			
			if (!questions.get(i).getCategory().isEmpty())
				cIds.add(questions.get(i).getCategory());
		}
		
		//Get authors
		AuthorModel authorModel = AuthorModel.getInstance();
		Map<String, Author> authors = authorModel.getByListIds(aIds);
		
		//Get categories
		CategoryModel categoryModel = CategoryModel.getInstance();
		Map<String, Category> categories = categoryModel.getByListIds(cIds);
		
		request.setAttribute("questions", questions);
		request.setAttribute("authors", authors);
		request.setAttribute("categories", categories);
		request.setAttribute("paginator", paginator);
		request.setAttribute("prevPage", prevPage);
		request.setAttribute("nextPage", nextPage);
		request.setAttribute("page", page);
		request.setAttribute("limit", limit);
		try {
			request.getRequestDispatcher("/templates/" + config.getTemplate() + "/pages/home.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
